﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Layout;

namespace WHControlLib.Forms
{
    public partial class baseForm : Form
    {
     /// <summary>
     /// 本基类只做参考，最好不要继承，不然设计器 不会设计背景图
     /// </summary>
        public baseForm()
        {

            InitializeComponent();

            this.DoubleBuffer(this.Controls);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

        }

        public void DoubleBuffer(Control.ControlCollection controls)
        {
            foreach (Control control in (ArrangedElementCollection)controls)
            {
                if (control.HasChildren)
                    this.DoubleBuffer(control.Controls);
                control.GetType().GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic).SetValue((object)control, (object)true);
            }
        }



        #region 减少闪烁代码，只能复制到主窗体中 ，不能继承 否则无法在VS设计器中对界面设计会阻断 设计器绘制设计界面背景图

      
            protected override void WndProc(ref Message msg)
        {
            if (msg.Msg == 513)
            {
                msg.Msg = 161;
                msg.LParam = IntPtr.Zero;
                msg.WParam = new IntPtr(2);
            }
            base.WndProc(ref msg);
        }
     

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams createParams = base.CreateParams;
                //if (this.Minimized)
                if (this.WindowState==FormWindowState.Minimized)
                

                
                {
                    createParams.Style |= 131072;
                    return createParams;
                }
                createParams.ExStyle |= 33554432;
                return createParams;
            }
        }

        
        
        #endregion



   //private bool _minimized;
   //     public bool Minimized
   //     {
   //         get => this._minimized;
   //         set
   //         {
   //             if (this._minimized == value)
   //                 return;
   //             this._minimized = value;
   //         }
   //     }
    







    }
}
